import ArgumentType from './extension-support/argument-type';
import BlockType from './extension-support/block-type';
import blockIconURI from './image/icon.svg';



class YourExtension{
    constructor(runtime) {
        this.runtime = runtime;
    }

    getInfo () {
        return {
            name: '算法库',
            blockIconURI: blockIconURI,
            blocks: [
                {  
                    opcode: 'strSimilarity2Percent',
                    blockType: BlockType.REPORTER,
                    text: '匹配度 [TEXT1] [TEXT2] ',
                    arguments: {
                        TEXT1: {
                            type: ArgumentType.STRING
                        },
                       
                        TEXT2: {
                            type: ArgumentType.STRING
                        }
                    }
                },
                {  
                    opcode: 'numberToChinese',
                    blockType: BlockType.REPORTER,
                    text: '数字转大写 [TEXT1] ',
                    arguments: {
                        TEXT1: {
                            type: ArgumentType.STRING
                        }
                    }
                }
            ]
        }
    }


    // 删除字符串中的标点符号和空格
    preprocessString(str) {
        // 使用正则表达式去掉标点符号和空格
        return str.replace(/[\s\p{P}]/gu, ''); // `\p{P}` 匹配 Unicode 标点符号，`\s` 匹配空格
    }

    strSimilarity2Number (s, t) {
        var n = s.length, m = t.length, d = [];
        var i, j, s_i, t_j, cost;
        if (n == 0) return m;
        if (m == 0) return n;
        for (i = 0; i <= n; i++) {
            d[i] = [];
            d[i][0] = i;
        }
        for (j = 0; j <= m; j++) {
            d[0][j] = j;
        }
        for (i = 1; i <= n; i++) {
            s_i = s.charAt(i - 1);
            for (j = 1; j <= m; j++) {
                t_j = t.charAt(j - 1);
                if (s_i == t_j) {
                    cost = 0;
                } else {
                    cost = 1;
                }
                d[i][j] = this.Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
            }
        }
        return d[n][m];
    }
    //两个字符串的相似程度，并返回相似度百分比
    strSimilarity2Percent (args) {
        var s = this.preprocessString(args.TEXT1)
        var t = this.preprocessString(args.TEXT2)
        t = this.preprocessString(t)
        var l = s.length > t.length ? s.length : t.length;
        var d = this.strSimilarity2Number(s, t);
        return (1 - d / l).toFixed(4);
    }



    numberToChinese(args) {

        const num = args.TEXT1;

        const units = ["", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿"];
    
    
        const chars = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
    
    
        const numStr = num.toString();
    
    
        let result = "";
    
    
        for (let i = 0; i < numStr.length; i++) {
    
    
            const n = numStr.charAt(i);
    
    
            result += chars[parseInt(n)] + units[numStr.length - 1 - i];
    
    
        }
    
    
        return result.replace(/零(拾|佰|仟|万|亿)/g, "零").replace(/零+/g, "零").replace(/零$/, "");
    
    
    }
    
    
    Minimum (a, b, c) {
        return a < b ? (a < c ? a : c) : (b < c ? b : c);
    }
}
module.exports = YourExtension;